Back to Main Menu

Asset Tools - (Assetic Python SDK)

Introduction

The Assetic Python SDK contains a set of tools that simplify common integration tasks for assets.  An asset is comprised of the asset record itself and within the asset are components.  Within each component are network measures (see also Asset Integration Overview).

The Asset Tools wrap this data structure into a single object representation "assetic.AssetToolsCompleteAssetRepresentation".  This allows all details of an asset to be prepared and passed to a single tool for complete asset creation or update.  This representation is also used when retrieving an asset via these tools.

Asset Tools Module

The Assetic Python SDK contains a module called assetic.AssetTools

 

Within that module are the following key methods:

  • get_complete_asset
  • create_complete_asset
  • update_complete_asset

 

AssetToolsCompleteAssetRepresentation

The Asset representation has the following structure:

  • asset_representation
    • an instance of "assetic.Assetic3IntegrationRepresentationsComplexAssetRepresentation"
  • components
    • a list array of "AssetToolsComponentRepresentation" instances

Within each "component" instance is:

  • component_representation
    • an instance of "assetic.Assetic3IntegrationRepresentationsComponentRepresentation"
  • dimensions
    • a list array of "Assetic3IntegrationRepresentationsComponentDimensionRepresentation"

Get Complete Asset

The following code sample gets an asset, including components and dimension details

 

  1. """
  2. Get an asset including component and dimensions using the SDK asset tools
  3. (Assetic.AssetsGetCompleteSDK.py)
  4. """
  5. import assetic
  6. #Assetic SDK instance
  7. asseticsdk=assetic.AsseticSDK("c:/users/you/assetic.ini", None,"Info")
  8. #Tools API
  9. assettools = assetic.AssetTools()
  10. assetid = "411142055"
  11. attributes = ["Zone","Comment"]
  12. asset = assettools.get_complete_asset(assetid,attributes)
  13. print(asset)

Update Complete Asset

To simplify the update process, the Assetic Python SDK handles the complexity of all the core fields being updated.  The following code sample utilises the Assetic Python SDK tool "update_complete_asset".  This tool wraps the API endpoints for asset, component, and dimension creation.  It gets the current values for the asset, component, and dimension prior to the update and applies those values to fields that are NULL in the update object passed into this method.  The update object is an instance of "assetic.AssetToolsCompleteAssetRepresentation".  This object allows setting of the asset, component, and dimension values that are to be updated, values that are not to be updated are left as NULL in the object.

 

If only the asset is to be updated then the "assetic.AssetToolsComponentRepresentation" object within the "assetic.AssetToolsCompleteAssetRepresentation" instance is left as an empty array.  Likewise the dimension object within "assetic.AssetToolsComponentRepresentation" may be left as NULL if the component is to be updated but not the dimensions.

 

The following code sample updates an asset with components and dimensions. 

 

  1. """
  2. Update an asset, component and dimensions
  3. (Assetic.AssetsPutComplete.py)
  4. """
  5. import assetic
  6. # Define location of configuration file, log file, log level
  7. asseticsdk=assetic.AsseticSDK("c:/users/you/assetic.ini", None, "Info")
  8. # Asset Tools API
  9. assettools = assetic.AssetTools()
  10. def main(assetguid,componentguid,dimensionguid):
  11. """
  12. Update the asset, components, and component dimensions
  13. """
  14. ##instantiate the complete asset representation
  15. complete_asset_obj = assetic.AssetToolsCompleteAssetRepresentation()
  16. #create an instance of the complex asset object
  17. asset = assetic.Assetic3IntegrationRepresentationsComplexAssetRepresentation()
  18. ##set asset guid to update
  19. asset.id = assetguid
  20. ##set fields to update
  21. asset.asset_external_identifier = "upd"
  22. attributes = {"Comment":"a revised comment","Link":"www.assetic.com"}
  23. asset.attributes = attributes
  24. ##Set the asset_representation to the defined asset values
  25. complete_asset_obj.asset_representation = asset
  26. ##array list to put component plus dimensions
  27. components_and_dims = []
  28. #instantiate complete component representation
  29. component_obj = assetic.AssetToolsComponentRepresentation()
  30. ##create an instance of the component representation
  31. component = assetic.Assetic3IntegrationRepresentationsComponentRepresentation()
  32. component.id = componentguid
  33. ##set fields to update
  34. component.design_life = 50
  35. component_obj.component_representation = component
  36. ##create an array for the dimensions to be added to the component
  37. dimlist = []
  38. ##Create an instance of the dimension and set minimum fields
  39. dim = assetic.Assetic3IntegrationRepresentationsComponentDimensionRepresentation()
  40. #set the id fields
  41. dim.id = dimensionguid
  42. dim.component_id = componentguid
  43. #set the field to update
  44. dim.network_measure = 5
  45. ##append to dimension array
  46. dimlist.append(dim)
  47. ##Add the dimension array to the component
  48. component_obj.dimensions = dimlist
  49. ##Add component to the list
  50. components_and_dims.append(component_obj)
  51. ##add the component & dims array to the complete asset object
  52. complete_asset_obj.components = components_and_dims
  53. ##update the complete asset
  54. errcode = assettools.update_complete_asset(complete_asset_obj,False)
  55. if errcode > 0:
  56. return
  57. print("success")
  58. ##This will run the method main() to kickstart it all
  59. if __name__ == "__main__":
  60. #set the IDs
  61. assetguid = "88439a40-6e1e-e711-946c-06edd62954d7"
  62. componentguid = "8e439a40-6e1e-e711-946c-06edd62954d7"
  63. dimensionguid = "90439a40-6e1e-e711-946c-06edd62954d7"
  64. #execute the main function
  65. main(assetguid,componentguid,dimensionguid)

Note that in the above sample there were updates to network measure, components, and the asset itself.

Create Asset

The following code sample utilises the Assetic Python SDK tool "create_complete_asset".  This tool wraps the API endpoints for asset, component, and dimension creation.

 

This code sample illustrates the creation of an asset, 2 components within that asset, and 2 dimension records within each component.

 

It utilises the "assetic.AssetToolsCompleteAssetRepresentation" object model to define the asset, components, and dimensions.  There are checks made to ensure the mandatory fields are not empty.

 

The response is the same object with the generated ID's included.

It is not mandatory for dimensions and components to be defined in the assetic.AssetToolsCompleteAssetRepresentation object.

 

  1. """
  2. Create an asset, component and dimensions
  3. (Assetic.AssetsPostComplete.py)
  4. """
  5. import assetic
  6. from datetime import datetime #use to generate a unique asset id
  7. # Assetic SDK instance
  8. asseticsdk=assetic.AsseticSDK("c:/users/you/assetic.ini",None,"Debug")
  9. #Asset Tools API
  10. assettools = assetic.AssetTools()
  11. def main():
  12. """
  13. Create the asset, components, and component dimensions
  14. Assumes the category does not have autonumber
  15. """
  16. ##generate an id based on the current time
  17. dt = datetime.now()
  18. assetid = "{0}{1}{2}{3}{4}".format(
  19. dt.month,dt.day,dt.hour,dt.minute,dt.second)
  20. ##instantiate the complete asset representation
  21. complete_asset_obj = assetic.AssetToolsCompleteAssetRepresentation()
  22. #create an instance of the complex asset object
  23. asset = assetic.Assetic3IntegrationRepresentationsComplexAssetRepresentation()
  24. ##mandatory fields
  25. asset.asset_category="Roads"
  26. asset.status = "Active"
  27. asset.asset_id = assetid
  28. asset.asset_name = "RD{0}".format(assetid)
  29. complete_asset_obj.asset_representation = asset
  30. ##array list to put component plus dimensions
  31. components_and_dims = []
  32. #instantiate complete component representation
  33. component_obj = assetic.AssetToolsComponentRepresentation()
  34. ##create an instance of the component representation
  35. component = assetic.Assetic3IntegrationRepresentationsComponentRepresentation()
  36. componentname = "{0}CMP1".format(assetid)
  37. component.asset_id = assetid
  38. component.label = "Main Label" #"Component Name in UI
  39. component.component_type = "Main"
  40. component.dimension_unit = "Metre"
  41. component.network_measure_type = "Length"
  42. #optional fields
  43. component.design_life = 50
  44. component.external_identifier = "Ext{0}".format(componentname)
  45. component.material_type = "Concrete" #must exist
  46. ##Add the component to the components
  47. component_obj.component_representation = component
  48. ##create an array for the dimensions to be added to the component
  49. dimlist = []
  50. ##Create an instance of the dimension and set minimum fields
  51. dim = assetic.Assetic3IntegrationRepresentationsComponentDimensionRepresentation()
  52. dim.network_measure = 75.7
  53. dim.unit = "Metre"
  54. dim.record_type = "Info" #could also be "Subtraction" or "Info"
  55. dim.network_measure_type = "Length"
  56. ##can also include additional fields
  57. dim.comments = "Created via API"
  58. dim.multiplier = 2.5 #will default as 1 if undefined
  59. dimlist.append(dim)
  60. ##Create an instance of the dimension and set minimum fields
  61. dim = assetic.Assetic3IntegrationRepresentationsComponentDimensionRepresentation()
  62. dim.network_measure = 3
  63. dim.unit = "Metre"
  64. dim.record_type = "Subtraction" #could also be "Subtraction" or "Info"
  65. dim.network_measure_type = "Length"
  66. ##can also include additional fields
  67. dim.comments = "Created via API"
  68. dim.multiplier = 1 #will default as 1 if undefined
  69. dimlist.append(dim)
  70. ##Add the dimension array to the component
  71. component_obj.dimensions = dimlist
  72. ##Add component to the list
  73. components_and_dims.append(component_obj)
  74. #instantiate complete component representation
  75. component_obj = assetic.AssetToolsComponentRepresentation()
  76. ##Create another component
  77. componentname = "{0}CMP2".format(assetid)
  78. component = assetic.Assetic3IntegrationRepresentationsComponentRepresentation()
  79. component.name = componentname
  80. component.asset_id = assetid
  81. component.label = "Formation"
  82. component.component_type = "Formation"
  83. component.dimension_unit = "Metre"
  84. component.network_measure_type = "Length"
  85. #optional fields
  86. component.design_life = 50
  87. component.external_identifier = "Ext{0}".format(componentname)
  88. #component.material_type = "Concrete" #must exist
  89. component_obj.component_representation = component
  90. ##create an array for the dimensions to be added to the component
  91. dimlist = []
  92. ##Create an instance of the dimension and set minimum fields
  93. dim = assetic.Assetic3IntegrationRepresentationsComponentDimensionRepresentation()
  94. dim.network_measure = 50
  95. dim.component_id = componentname
  96. dim.unit = "Metre"
  97. dim.record_type = "Addition" #could also be "Subtraction" or "Info"
  98. dim.network_measure_type = "Length"
  99. ##can also include additional fields
  100. dim.comments = "Created via API"
  101. dim.multiplier = 2 #will default as 1 if undefined
  102. dimlist.append(dim)
  103. ##Create an instance of the dimension and set minimum fields
  104. dim = assetic.Assetic3IntegrationRepresentationsComponentDimensionRepresentation()
  105. dim.network_measure = 5
  106. dim.component_id = componentname
  107. dim.unit = "Metre"
  108. dim.record_type = "Subtraction" #could also be "Subtraction" or "Info"
  109. dim.network_measure_type = "Length"
  110. ##can also include additional fields
  111. dim.comments = "Created via API"
  112. dim.multiplier = 3 #will default as 1 if undefined
  113. dimlist.append(dim)
  114. ##Add the dimension array to the component
  115. component_obj.dimensions = dimlist
  116. ##Add component with dims to the list
  117. components_and_dims.append(component_obj)
  118. ##add the component & dims array to the complete asset object
  119. complete_asset_obj.components = components_and_dims
  120. ##create the complete asset
  121. response = assettools.create_complete_asset(complete_asset_obj)
  122. if len(response.components) > 0:
  123. #get some ID's
  124. assetguid = response.asset_representation.id
  125. #first component
  126. componentguid = response.components[0].component_representation.id
  127. #first dimension
  128. dimensionguid = response.components[0].dimensions[0].id
  129. def create_asset(assetid,category,name,status):
  130. """
  131. Create an asset for the given asset ID and category
  132. :param assetid: Assetic user friendly asset ID
  133. :param assetid: asset category name
  134. :param assetid: asset name
  135. :param assetid: status - "Active" or "Pending"
  136. :return: asset response object or None
  137. """
  138. asseticsdk.logger.info("Create the asset {0}".format(assetid))
  139. #create an instance of the complex asset object
  140. asset = assetic.Assetic3IntegrationRepresentationsComplexAssetRepresentation()
  141. ##mandatory fields
  142. asset.asset_category=category
  143. asset.status = status
  144. asset.asset_id = assetid
  145. asset.asset_name = name
  146. ##now execute the request
  147. try:
  148. newasset = assetapi.complex_asset_post(asset)
  149. except assetic.rest.ApiException as e:
  150. if e.status == 400:
  151. msg = "Bad Request. Status {0}, Reason: {1} {2}".format(
  152. e.status,e.reason,e.body)
  153. asseticsdk.logger.error(msg)
  154. else:
  155. asseticsdk.logger.error("Status {0}, Reason: {1} {2}".format(
  156. e.status,e.reason,e.body))
  157. return None
  158. return newasset
  159. def create_component(assetid,componentname):
  160. """
  161. Create an component for the given asset ID and component name
  162. :param assetid: Assetic component GUID or user friendly component ID
  163. :param componentname: Assetic component name
  164. :return: component response object or None
  165. """
  166. asseticsdk.logger.info("Create the component {0}".format(componentname))
  167. #create instance of component and set minimum values
  168. component = assetic.Assetic3IntegrationRepresentationsComponentRepresentation()
  169. component.name = componentname
  170. component.asset_id = assetid
  171. component.label = "Main"
  172. component.component_type = "Main"
  173. component.dimension_unit = "Metre"
  174. component.network_measure_type = "Length"
  175. #optional fields
  176. component.design_life = 50
  177. component.external_identifier = "Ext{0}".format(componentname)
  178. component.material_type = "Concrete" #must exist
  179. try:
  180. component = componentapi.component_post(component)
  181. except assetic.rest.ApiException as e:
  182. if e.status == 400:
  183. msg = "Component for Component GUID {0} not found".format(assetguid)
  184. asseticsdk.logger.error(msg)
  185. else:
  186. msg = "Status {0}, Reason: {1} {2}".format(e.status,e.reason,e.body)
  187. asseticsdk.logger.error(msg)
  188. return None
  189. return component
  190. def create_dimension(componentid,measure):
  191. """
  192. Create a dimension for the given component ID
  193. :param assetid: Assetic component GUID or user friendly component ID
  194. :param measure: The measure to apply to the dimension
  195. :return: dimension response object or None
  196. """
  197. asseticsdk.logger.info("Create the dimensions for component {0}".format(
  198. componentid))
  199. ##Create an instance of the dimension and set minimum fields
  200. dim = assetic.Assetic3IntegrationRepresentationsComponentDimensionRepresentation()
  201. dim.network_measure = measure
  202. dim.unit = "Metre"
  203. dim.record_type = "Addition" #could also be "Subtraction" or "Info"
  204. dim.network_measure_type = "Length"
  205. ##can also include additional fields
  206. dim.comments = "Created via API"
  207. dim.multiplier = 2 #will default as 1 if undefined
  208. try:
  209. newdim = componentapi.component_post_dimension(componentid,dim)
  210. except assetic.rest.ApiException as e:
  211. if e.status == 404:
  212. msg = "Dimensions for Component {0} not found".format(assetguid)
  213. asseticsdk.logger.error(msg)
  214. else:
  215. msg = "Status {0}, Reason: {1} {2}".format(e.status,e.reason,e.body)
  216. asseticsdk.logger.error(msg)
  217. return None
  218. return newdim
  219. ##This will run the method main() to kickstart it all
  220. if __name__ == "__main__":
  221. main()